package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.resolver.annotation.SsoTeacher;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.entity.Teachlink;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.teachlink.service.TeachlinkService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.TeachLinkParams;
import com.lanou3g.ptp.vo.TeachlinkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(description = "教学环节")
@RestController
@Slf4j
public class TeachlinkController {

    @Reference
    private SsoService ssoService;

    @Reference
    private TeachlinkService teachlinkService;

    /**
     * @param condition
     * @return
     * @author 武奇
     */
    @ApiOperation("查询所有教学环节")
    @PostMapping("/teachlinks")
    public PtpResult find(@RequestBody TeachLinkParams condition) {
        PageInfo<Teachlink> p = this.teachlinkService.findByCondition(condition);
        return PtpResultUtils.ok(p);
    }

    @ApiOperation("根据id查询教学环节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教学环节id")
    })
    @GetMapping("/teachlink/{id}")
    public PtpResult findById(@PathVariable Long id) {
        TeachlinkVO vo = teachlinkService.findBodyById(id);
        return PtpResultUtils.ok(vo);
    }

    /**
     * @param token
     * @param teachlink
     * @return
     * @author 武奇
     */
    @ApiOperation("添加教学环节")
    @PostMapping("/teachlink")
    public PtpResult insert(@RequestHeader String token, @RequestBody TeachlinkVO teachlink) {
        log.info("添加教学环节, 接收数据: {}", teachlink);
        Teacher teacher = (Teacher) this.ssoService.tokenUser(token);
        log.info("添加教学环节, 当前登录Teacher信息: {}", teacher);
        teachlinkService.insert(teacher.getId(), teachlink);
        return PtpResultUtils.ok();
    }

    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教学环节id")
    })
    @DeleteMapping("/teachlink/{id}")
    public PtpResult delete(@PathVariable Long id) {
        teachlinkService.deleteById(id);
        return PtpResultUtils.ok();
    }

    @ApiOperation("根据id删除多个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "教学环节id数组")
    })
    @DeleteMapping("/teachlink")
    public PtpResult deletes(@RequestBody Long[] ids) {
        teachlinkService.deleteByIds(ids);
        return PtpResultUtils.ok();
    }

    @ApiOperation("修改教学环节")
    @PutMapping("/teachlink")
    public PtpResult update(@SsoTeacher Teacher teacher, @RequestBody TeachlinkVO teachlink) {
        teachlinkService.updateVO(teacher.getId(), teachlink);
        return PtpResultUtils.ok();
    }
}
